package logic

import (
	"context"
	"fmt"
	"teacher1/model/mysql"
	"teacher1/pkg"
	"teacher1/teacherrpc/internal/svc"
	"teacher1/teacherrpc/teacherrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type TeacherLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTeacherLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TeacherLoginLogic {
	return &TeacherLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TeacherLoginLogic) TeacherLogin(in *teacherrpc.TeacherLoginRequest) (*teacherrpc.TeacherLoginResponse, error) {
	// todo: add your logic here and delete this line
	var teacher mysql.Teacher
	mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&teacher)
	if teacher.ID == 0 {
		return nil, fmt.Errorf("当前账号不存在")
	}
	if teacher.Password != pkg.MD5(in.Password) {
		return nil, fmt.Errorf("密码错误，请重新输入")
	}
	return &teacherrpc.TeacherLoginResponse{Id: int64(teacher.ID)}, nil
}
